package leetcode.每日一题;

import leetcode.剑指offer.pojo.TreeNode;
import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/3/19 15:00
 * @description：https://leetcode-cn.com/problems/construct-string-from-binary-tree/
 */
public class 根据二叉树创建字符串 {
    @Test
    public void test() {
        Solution solution = new Solution();
    }

    class Solution {
        public String tree2str(TreeNode root) {
            StringBuilder builder = new StringBuilder();
            if (root == null) return "";
            builder.append(root.val);
            if (root.left != null || root.right != null) {
                builder.append("(");
                builder.append(tree2str(root.left));
                builder.append(")");
            }
            if (root.right != null) {
                builder.append("(");
                builder.append(tree2str(root.right));
                builder.append(")");
            }
            return builder.toString();
        }
    }
}
